package com.group.dao;

import com.group.model.ClockinChallenge;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ClokinDao {

    /**
     * 是否参加过报名
     *
     * @param userId
     * @param joinTime
     * @return
     */
    @Select("select * from clockinchallenge c where c.userId=#{userId} and c.joinTime = #{joinTime}")
    public ClockinChallenge queryIsJoin(@Param("userId") Long userId, @Param("joinTime") String joinTime);

    /**
     * 瓜分金额
     *
     * @param joinTime
     * @return
     */
    @Select("select ifnull(sum(c.putWecc) ,0) from clockinchallenge c where c.joinTime = #{joinTime}")
    public Double queryCarveUpWecc(@Param("joinTime") String joinTime);


    /**
     * 报名参加人数
     *
     * @param joinTime
     * @return
     */
    @Select("select count(*) from clockinchallenge c where c.joinTime = #{joinTime}")
    public int queryJoinPeople(@Param("joinTime") String joinTime);

    /**
     * 获取头像地址
     *
     * @param joinTime
     * @return
     */
    @Select("select c.userHeadimage from clockinchallenge c where c.joinTime = #{joinTime} order by c.createTime desc limit #{start},#{limit}")
    public List<String> queryUserHeadimages(@Param("joinTime") String joinTime, @Param("start") Integer start, @Param("limit") Integer limit);


    /**
     * 打卡成功/失败 的战况总数
     *
     * @param status
     * @param joinTime
     * @return
     */
    @Select("select count(*) from clockinchallenge c where c.status =#{status} and c.joinTime = #{joinTime}")
    public int queryStatusNum(@Param("status") String status, @Param("joinTime") String joinTime);

    /**
     * 早起之星
     *
     * @param joinTime
     * @return
     */
    @Select("select c.clockinTime from clockinchallenge c  where c.joinTime = #{joinTime} and c.status = '早起成功' order by c.clockinTime limit 1")
    public String queryEarlyStar(@Param("joinTime") String joinTime);

    /**
     * 手气之星
     *
     * @param joinTime
     * @return
     */
    @Select("select c.incomeWecc from clockinchallenge c  where c.joinTime = #{joinTime} and c.status = '早起成功' order by c.incomeWecc desc limit 1")
    public Double queryLuckStar(@Param("joinTime") String joinTime);

    /**
     * 毅力之星
     *
     * @return
     */
    @Select("select u.clikinCount from user u order by u.clikinCount desc limit 1")
    public int queryWillpowerStar();

    /**
     * 统计当天 投入的Wecc总和
     *
     * @param joinTime
     * @return
     */
    @Select("select ifnull(sum(c.putWecc) ,0) from clockinchallenge c where c.joinTime = #{joinTime} and c.status = #{status}")
    public Double queryPutWeccSum(@Param("joinTime") String joinTime, @Param("status") String status);


    /**
     * 成功/失败的参与者
     *
     * @param joinTime
     * @return
     */
    @Select("select * from clockinchallenge c where c.joinTime = #{joinTime} and c.status = #{status}")
    public List<ClockinChallenge> queryDayPersons(@Param("joinTime") String joinTime, @Param("status") String status);

    /**
     * 我的战绩 每日明细
     *
     * @param userId
     * @param start
     * @return
     */
    @Select("select * from clockinChallenge c where c.userId = #{userId} order by c.joinTime desc limit #{start},#{limit}")
    public List<ClockinChallenge> queryMyEveryDetail(@Param("userId") Long userId, @Param("start") Integer start, @Param("limit") Integer limit);

    /**
     * 个人总投入
     *
     * @param userId
     * @return
     */
    @Select("select ifnull(sum(c.putWecc) ,0) from clockinchallenge c  where c.userId = #{userId}")
    public Double queryMyPutWeccSum(@Param("userId") Long userId);

    /**
     * 个人总收入
     *
     * @param userId
     * @return
     */
    @Select("select ifnull(sum(c.incomeWecc) ,0) from clockinchallenge c  where c.userId = #{userId}")
    public Double queryMyIncomeWeccSum(@Param("userId") Long userId);

    /**
     * 个人参与报名次数
     *
     * @param userId
     * @return
     */
    @Select("select count(*) from clockinchallenge c  where c.userId = #{userId}")
    public int queryMyJoinSum(@Param("userId") Long userId);

    /**
     * 个人打卡成功次数
     *
     * @param userId
     * @return
     */
    @Select("select count(*) from clockinchallenge c  where c.userId = #{userId} and c.status = '早起成功'")
    public int queryMySuccessSum(@Param("userId") Long userId);

    /**
     * 设置失败状态
     *
     * @param joinTime
     */
    @Update("update clockinchallenge c set c.status='早起失败' where joinTime=#{joinTime} and c.status != '早起成功'")
    public void updateFailedStatus(@Param("joinTime") String joinTime);

    /**
     * 连续签到次数：打卡成功者+1
     *
     * @param joinTime
     */
    @Update("update user u , clockinchallenge c set u.clikinCount = u.clikinCount+1 where u.id = c.userId and c.status = '早起成功' and joinTime=#{joinTime}")
    public void updateSuccessClikinCount(@Param("joinTime") String joinTime);

    /**
     * 连续签到次数：打卡失败者置0
     *
     * @param joinTime
     */
    @Update("update user u , clockinchallenge c set u.clikinCount = 0 where u.id != c.userId and c.status = '早起成功' and joinTime=#{joinTime}")
    public void updateFailedClikinCount(@Param("joinTime") String joinTime);

}
